#!/usr/bin/perl
#
# Create the country.story file from the statistics table
#
# Inverse of story2db - this + snapshot are intended to replace
# the "fieldstory" tool
#
use Counter;
use POSIX;

my $stats = Counter::openfile("statistics");
@statlist = $stats->select("name='persons' AND
        (qualifier like 'country=%' OR qualifier = '' OR qualifier like 'All%')
                           order by date, qualifier");
for $stat (@statlist) {
    my $q = $stat->{qualifier};
    if ($q eq "") {
	$q = "All";
    } elsif ($q eq "All - freeze candidates") {
	$q = "Real";
    }
    $q =~ s/^country=//;
    print substr($stat->{date}, 0, 10), ":", $q, ":", $stat->{value}, "\n";
    # lengthen the "Real" curve after we've stopped generating it
    if ($q eq "All" && $stat->{date} gt '2005-01-08') {
	print substr($stat->{date}, 0, 10), ":", "Real", ":", $stat->{value}, "\n";
    }
}
